package com.engineering.elink.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.co.CommentCo;
import com.engineering.elink.common.CoinEnum;
import com.engineering.elink.common.ErrorType;
import com.engineering.elink.common.Pagination;
import com.engineering.elink.dao.CommentMapper;
import com.engineering.elink.dao.FileMapper;
import com.engineering.elink.dao.PraiseMapper;
import com.engineering.elink.domain.Comment;
import com.engineering.elink.domain.File;
import com.engineering.elink.domain.Praise;
import com.engineering.elink.hessian.CoinHessianService;
import com.engineering.elink.service.CommentService;
import com.engineering.elink.util.JSONUtil;

@Service("commentService")
public class CommentServiceImpl implements CommentService {
	@Autowired
	private CommentMapper commentMapper;
	@Autowired
	private FileMapper fileMapper;
	@Autowired
	private PraiseMapper praiseMapper;
	@Autowired
	private CoinHessianService coinHessianService;
	@Override
	public Map<String, Object> getTopCommentsByFileId(CommentCo co) {
		Map<String , Object>map= new HashMap<String, Object>();
		
		map.put("list",commentMapper.selectTopCommentsByFileId(co));
		map.put("Pagination", new Pagination(co, commentMapper.selectTopCommentsCount(co)));
		return JSONUtil.getJSON(ErrorType.SUCCESS,map);
	}
	
	@Override
	public Map<String, Object> getAllCommentsByTopId(BaseCo bc, int topId) {
		Map<String , Object>map= new HashMap<String, Object>();
		
		map.put("list",commentMapper.selectAllCommentsByTopId(bc,topId));
		map.put("Pagination", new Pagination(bc, commentMapper.selectCount(bc,topId)));
		return JSONUtil.getJSON(ErrorType.SUCCESS,map);
	}
	
	
	@Override
	public Map<String, Object> add(String fileId,String userId, String content) {
		File file = fileMapper.selectByPrimaryKey(fileId);
		if(file != null){
			//一个用户只能够评论一次
			int count = commentMapper.selectDistinctCount(fileId,userId);
			if(count>0){
				return JSONUtil.getJSON(ErrorType.COMMENT_ERROR);
			}
			int  commentId = commentMapper.selectMaxCommentId();
			Comment c = new Comment();
			c.setCommentId(commentId+1);
			c.setUserId(userId);
			c.setContent(content);
			c.setFileId(fileId);
			c.setByUserId(file.getUserId());
			c.setTime(new Date());
			c.setParentId(0);//一级评论的父类id为0
			c.setTopId(0); //一级评论的层级topId为0
			commentMapper.insertSelective(c);
			
			//更新file的评论数量
			file.setCommentCount(file.getCommentCount()+1);
			fileMapper.updateByPrimaryKeySelective(file);
			return JSONUtil.getJSON(ErrorType.SUCCESS);
		}else{
			return JSONUtil.getJSON(ErrorType.FILE_COMMENT_ERROR);
		}
	}
	
	@Override
	public Map<String, Object> addReply(String fileId,int commentId, String userId,String content) {
		Comment c = commentMapper.selectByPrimaryKey(commentId);
		if(c != null){
			//一个用户只能够回复一次
			int replyCount = commentMapper.selectReplyCount(fileId,userId);
			if(replyCount >0 ){
				return JSONUtil.getJSON(ErrorType.REPLY_ERROR);
			}
			int oldCommentId =  commentMapper.selectMaxCommentId();
			Comment c1 = new Comment();
			c1.setContent(content);
			c1.setTime(new Date());
			c1.setUserId(userId);
			c1.setContent(content);
			c1.setCommentId(oldCommentId+1);
			c1.setFileId(fileId);
			
			c1.setByUserId(c.getUserId());
			c1.setParentId(c.getCommentId());//父类id为被评论的评论id
			c1.setTopId(c.getTopId()+1);//为一级评论的Topid
			
			//更新file的评论数量
			File file = fileMapper.selectByPrimaryKey(fileId);
			file.setCommentCount(file.getCommentCount()+1);
			fileMapper.updateByPrimaryKeySelective(file);
			
			commentMapper.insertSelective(c1);
			return JSONUtil.getJSON(ErrorType.SUCCESS);
		}else{
			return JSONUtil.getJSON(ErrorType.COMMENT_REPLY_ERROR);
		}
	}

	@Override
	public Map<String, Object> addPraise(Praise praise) {
		int count = praiseMapper.select(praise.getCommentId(),praise.getType(),praise.getUserId());
		if(count == 0){
			Comment c  = commentMapper.selectByPrimaryKey(praise.getCommentId());
			if(c != null){
				praise.setPraiseTime(new Date());
				praiseMapper.insertSelective(praise);
				//更新评论的顶/踩数量
				if(praise.getType()==1){//顶
					c.setPraiseCount(c.getPraiseCount()+1);
				}else{//踩
					c.setStampCount(c.getStampCount()+1);
				}
				commentMapper.updateByPrimaryKeySelective(c);
				//送E币
				try {
					coinHessianService.modifyCoin(c.getFileId(), CoinEnum.PRAISEFILE.getIndex(), 0, praise.getUserId(), null);
				} catch (Exception e) {
					  Logger logger = Logger.getLogger(CommentServiceImpl.class);
					  logger.info("送E币失败"+e);
				}
				return JSONUtil.getJSON(ErrorType.SUCCESS,c);
			}else{
				return JSONUtil.getJSON(ErrorType.COMMENT_PRAISE_ERROR);
			}	
		}else {
			return JSONUtil.getJSON(ErrorType.PRAISE_ERROR);
		}
	}

	@Override
	public Map<String, Object> getCommentForMe(BaseCo co,String userId) {
		Map<String , Object>map= new HashMap<String, Object>();
		
		map.put("list",commentMapper.getCommentForMe(co ,userId));
		map.put("Pagination", new Pagination(co, commentMapper.selectCountCommentForMe(userId)));
		
		return JSONUtil.getJSON(ErrorType.SUCCESS,map);
	}

	@Override
	public Map<String, Object> getMyComment(BaseCo co,String userId) {
		Map<String , Object>map= new HashMap<String, Object>();
		
		map.put("list",commentMapper.getMyComment(co ,userId));
		map.put("Pagination", new Pagination(co, commentMapper.selectCountMyComment(userId)));
		
		return JSONUtil.getJSON(ErrorType.SUCCESS,map);
	}
}
